package com.breaktian.assemble.customviews.viewpager.cursorviewpager;

import android.content.Context;
import android.graphics.Color;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.breaktian.assemble.R;
import com.breaktian.assemble.utils.LogUtil;
import com.breaktian.assemble.utils.ViewUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tiankeke on 2017/6/21.
 * 带有游标的viewpager
 * 入口类
 */

public class CursorPagerView extends RelativeLayout{

    private LinearLayout ll_titles;
    private LinearLayout ll_corsor;
    private ViewPager viewPager;
    private Context mContext;
    private ImageView cursor;
    private List<CursorFragment> mCursorFragments;
    private RelativeLayout rl_cursor;
    private String mTitleNormalColor = "#464646";
    private String mTitleSelectColor = "#12C286";
    private View cursorLine;

    public CursorPagerView(Context context) {
        super(context);
        init(context);
    }

    public CursorPagerView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public CursorPagerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.bm_cursor_pager_view,this);
        ll_titles = (LinearLayout)findViewById(R.id.ll_titles);
        ll_corsor = (LinearLayout)findViewById(R.id.ll_cursor);
        rl_cursor = (RelativeLayout)findViewById(R.id.rl_cursor);
        cursorLine = findViewById(R.id.cursorLine);
        viewPager = (ViewPager)findViewById(R.id.viewPager);
        cursor = (ImageView)findViewById(R.id.cursor);



    }

    public void setFragments(FragmentManager fm, List<CursorFragment> cursorFragments,CursorParams params){
        setFragments(fm,cursorFragments);
        if(params!=null){
            setTitle(params.getTitleHeight(),
                    params.getTitleBackgroundColor(),
                    params.getTitleNormalColor(),
                    params.getTitleSelectColor());
            setCursor(
                    params.getCursorWidth(),
                    params.getCursorHeight(),
                    params.getCursorColor(),
                    params.getCursorBackgroundColor(),
                    params.getCursorLineColor(),
                    params.getCursorLineHeight()
                    );
        }
    }


    public void setFragments(FragmentManager fm, List<CursorFragment> corsorFragments){
        this.mCursorFragments = corsorFragments;
        if(corsorFragments==null||corsorFragments.size()==0){
            return;
        }
        List<Fragment> fragmentList = new ArrayList<>();
        List<String> fragmentTitles = new ArrayList<>();
        for (CursorFragment fragment:corsorFragments ) {
            fragmentList.add(fragment.getFragment());
            fragmentTitles.add(fragment.getTitle());
        }
        CursorPagerAdapter mCursorPagerAdapter = new CursorPagerAdapter(fm,fragmentList);
        viewPager.setAdapter(mCursorPagerAdapter);


        if(fragmentList.size()>0){
            ll_titles.setWeightSum(fragmentTitles.size());
            for (int i=0;i<fragmentTitles.size();i++){
                String title = fragmentTitles.get(i);
                final int position = i;
                TextView tv = new TextView(mContext);
                tv.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(position);
                    }
                });
                tv.setText(title);
                LinearLayout.LayoutParams tvParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                tvParams.weight = 1;
                tv.setGravity(Gravity.CENTER);
                tv.setLayoutParams(tvParams);
                ll_titles.addView(tv);
            }

        }

    }

    /**
     * @param width 单位dp
     * @param height 单位dp
     * @param color #CCCCCC
     * */
    public void setCursor(int width,int height,String color, String backgroundColor,String cursorLineColor,int cursorLineHeight){
        if(mCursorFragments==null||mCursorFragments.size()==0){
            LogUtil.e("请先设置fragment");
            return;
        }


        LinearLayout.LayoutParams paramsLine = (LinearLayout.LayoutParams)cursorLine.getLayoutParams();
        paramsLine.height = ViewUtil.dip2px(mContext,cursorLineHeight);
        cursorLine.setLayoutParams(paramsLine);
        try {
            cursorLine.setBackgroundColor(Color.parseColor(cursorLineColor));
        }catch (Exception e){
            e.printStackTrace();
        }

        LinearLayout.LayoutParams params1 = (LinearLayout.LayoutParams)rl_cursor.getLayoutParams();
        params1.width = ViewUtil.getScreenWidth(mContext)/mCursorFragments.size();
        rl_cursor.setLayoutParams(params1);

        RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) cursor.getLayoutParams();
        params.width = ViewUtil.dip2px(mContext,width);
        params.height = ViewUtil.dip2px(mContext,height);
        cursor.setLayoutParams(params);
        try {
            cursor.setBackgroundColor(Color.parseColor(color));
            if(!TextUtils.isEmpty(backgroundColor)){
                ll_corsor.setBackgroundColor(Color.parseColor(backgroundColor));
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        //初始化cursor
        int screenWidth = ViewUtil.getScreenWidth(mContext);
        final int offset = screenWidth / mCursorFragments.size();// 计算偏移量

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            int currentPostion = 0;

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Animation animation = new TranslateAnimation(currentPostion*offset,position*offset,0,0);
                animation.setFillAfter(true);// True:图片停在动画结束位置
                animation.setDuration(300);
                rl_cursor.startAnimation(animation);
                currentPostion = position;

                titleTextColorSelect(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void titleTextColorSelect(int position) {
        for(int i=0;i<ll_titles.getChildCount();i++){
            View view = ll_titles.getChildAt(i);
            if(view instanceof TextView){
                try {
                    if(i==position){
                        ((TextView)view).setTextColor(Color.parseColor(mTitleSelectColor));
                    }else{
                        ((TextView)view).setTextColor(Color.parseColor(mTitleNormalColor));
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }

    }

    /**
     * @param titleHeight 单位dp
     * */
    public void setTitle(int titleHeight,String background,String titleNormalColor,String titleSelectColor){
        if(mCursorFragments==null||mCursorFragments.size()==0){
            LogUtil.e("请先设置fragment");
            return;
        }
        if(!TextUtils.isEmpty(titleNormalColor)){
            this.mTitleNormalColor = titleNormalColor;
        }
        if(!TextUtils.isEmpty(titleSelectColor)){
            this.mTitleSelectColor = titleSelectColor;
        }
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) ll_titles.getLayoutParams();
        params.height = ViewUtil.dip2px(mContext,titleHeight);
        ll_titles.setLayoutParams(params);
        try {
            ll_titles.setBackgroundColor(Color.parseColor(background));
        }catch (Exception e){
            e.printStackTrace();
        }
        


    }



}
